package com.dq.Week3.day14;

import com.dq.Week3.day12.list.ArtShow;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HashMapDemo {
    public static void main(String[] args) throws FileNotFoundException {
        String address = "D:\\CSstr\\IP.txt";
        List<String> list = new ArrayList<>();
        File file = new File(address);
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            list.add(line);
        }
        //去重打印
        Set<String> strings = new HashSet<>(list);
        System.out.println("去重后");
        for (String string : strings) {

            System.out.println(string);
        }

        //循环存进map
        Map<String,Integer> map = new HashMap<>();
        for (String s : list) {
//            if(map.containsKey(s)==false) {
                Integer oldValue = map.get(s);
                map.put(s,oldValue==null?1:oldValue+1);
//                map.put(s, 1);
//            }else {
//                map.put(s,map.get(s)+1);
//            }
//            }
        }
        System.out.println("=========================================");
        System.out.println("未去重");
        for (String s1 : map.keySet()) {
            System.out.println("地址："+s1+"\t"+map.get(s1)+"个");
        }
        System.out.println("=========================================");
//        String[] strings1 = map.keySet().toArray(new String[0]);
//        int max = map.get(strings1[0]);
//        for (String s : map.keySet()) {
//            if(max<map.get(s))
//            {
//                max = map.get(s);
//            }
//        }
            Set<Map.Entry<String,Integer>> entries = map.entrySet();
            Map.Entry<String ,Integer> max = null;
        for (Map.Entry<String, Integer> entry : entries) {
            if(max==null)
            {
                max = entry;
            }else{
                if(max.getValue()<entry.getValue())
                {
                    max = entry;
                }
            }

        }
        System.out.println("地址："+max.getKey()+"出现次数最多");
        System.out.println("=========================================");
        System.out.println(map);
        Set<Map.Entry<String,Integer>> entries1 = map.entrySet();

        //TreeSet排序
        //指定Treeset中元素的大小关系 提供一个比较器对象 按照Entry的value部分进行大小比较
        TreeSet<Map.Entry<String,Integer>> treeSet = new TreeSet<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {

                return o2.getValue()-o1.getValue();
            }
        });
        int count = 0;
        treeSet.addAll(entries1);
        //展示treeset中的前三个元素
        for (Map.Entry<String, Integer> stringIntegerEntry : treeSet) {
            System.out.println(stringIntegerEntry.getKey()+":"+stringIntegerEntry.getValue());
            count++;
            if(count==3)
            {
                return;
            }

        }

        scanner.close();
    }


}
